//=============================================================================
/// Buffer Allocator
//=============================================================================

#ifndef __BMALLOC_H__
#define __BMALLOC_H__

#ifdef __cplusplus
extern "C" {
#endif

/// for alignment
typedef long BAllocAlign;

/// block header
union balloc_header {		/* block header */
	struct {
		union balloc_header* ptr;		/* pointer for next block */
		unsigned nunits;				/* size of this block */
	} s;
	BAllocAlign _align;		/* for alignment */
};

/// struct
typedef struct {
	union balloc_header base;		/* base point for free list */
	union balloc_header* freep;		/* free list */
	union balloc_header* used;		/* used list */
	union balloc_header* marked;	/* marked list (for GC) */

	int total_malloc_num;
	int total_free_num;
} BAlloc;

/// initialize
void balloc_init(BAlloc* balc, void* buf, unsigned size);

/// allocate memory from buffer
void *balloc(BAlloc* balc, unsigned nbytes);

/// free allocated memory
void bfree(BAlloc* balc, void* ap);

/// mark
int mark(BAlloc* balc, void* work);

/// sweep
void sweep(BAlloc* balc);

/// get information
/**
	@param	p_total_free_size	[out] 
	@param	p_max_block_size	[out] 

	@return		total allocated block num
*/
int balloc_get_info(BAlloc* balc, int* p_total_free_size, int* p_max_block_size);

#ifdef __cplusplus
} //extern "C"
#endif

#endif
